# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=crt
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn")
pkgver=5.0.0.4734.8f72872
pkgrel=1
pkgdesc='MinGW-w64 CRT for Windows'
arch=('any')
url='http://mingw-w64.sourceforge.net'
license=('custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-headers-git")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-binutils")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
options=('!strip' 'staticlibs' '!buildflags' '!emptydirs')
source=("mingw-w64"::"git://git.code.sf.net/p/mingw-w64/mingw-w64"
        "0003-dxgi-Add-missing-dxgi-1.2-structs-and-interfaces.patch"
        "0004-d3d11-Add-missing-d3d11-1.1-structs-and-interfaces.patch"
        "0005-handle-ctor_list-internally.patch")
sha256sums=('SKIP'
            '677642bbb0df3f1e0467f075f24c720657e9ed912a6e69cbf189352271fc09c1'
            'c6c996b273ee049a2c0b9570f007d2d76a95ff56abb3b147f156e6a221a561ef'
            '0eabe00375d4e26fd7e9afa56f6a9da02845fe2c0f900f5e1f829842bff8624a')

pkgver() {
  cd "${srcdir}/mingw-w64"
  local _major=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MAJOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 16 mingw-w64-headers/crt/_mingw_mac.h | grep '__MINGW64_VERSION_MINOR' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _rev=0
  printf "%s.%s.%s.%s.%s" ${_major} ${_minor} ${_rev} "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/mingw-w64
  git am --committer-date-is-author-date "${srcdir}"/0003-dxgi-Add-missing-dxgi-1.2-structs-and-interfaces.patch
  git am --committer-date-is-author-date "${srcdir}"/0004-d3d11-Add-missing-d3d11-1.1-structs-and-interfaces.patch
  #git apply -R --committer-date-is-author-date "${srcdir}"/0005-handle-ctor_list-internally.patch
}

build() {
  msg "Building ${MINGW_CHOST} CRT"
  case "$CARCH" in
    i686)
      _crt_configure_args="--disable-lib64 --enable-lib32"
    ;;
    x86_64)
      _crt_configure_args="--disable-lib32 --enable-lib64"
    ;;
  esac

  [[ -d ${srcdir}/crt-${MINGW_CHOST} ]] && rm -rf ${srcdir}/crt-${MINGW_CHOST}
  mkdir -p ${srcdir}/crt-${MINGW_CHOST} && cd ${srcdir}/crt-${MINGW_CHOST}
  ${srcdir}/mingw-w64/mingw-w64-crt/configure \
    --prefix=${MINGW_PREFIX}/${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-sysroot=${MINGW_PREFIX}/${MINGW_CHOST} \
    --enable-wildcard \
    ${_crt_configure_args}
  make
}

package() {
  msg "Installing ${MINGW_CHOST} crt"
  cd ${srcdir}/crt-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install-strip
}
